-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate towhat policy field #18032
Validate towhat policy field #18032
Conversation
@miq-bot add_label bug |
@miq-bot add_label hammer/yes |
app/models/miq_policy.rb
Outdated
@@ -1,6 +1,16 @@ | |||
# TODO: Import/Export support | |||
|
|||
class MiqPolicy < ApplicationRecord | |||
TOWHAT_APPLIES_TO_CLASSES = %w(Host |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be sorted alphabetically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 will do
spec/models/miq_policy_spec.rb
Outdated
|
||
context '.validates' do | ||
it 'validates towhat and mode' do | ||
expect(described_class.create!(:towhat => "Host", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to save the record to test this, you can
expect(FactoryGirl.build(:miq_policy, :towhat => "Host", :mode => "compliance").valid?).to be true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should mode
even be tested here since it didn't change in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm usually not a huge fan of this syntax, but you could also
expect(FactoryGirl.build(:miq_policy, :towhat => "Host")).to be_valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdunne Thank you for the input. I'll have the new test focus on towhat
I like the syntax the way it is and if you don't have strong feelings about it I'd like to leave it as is, mostly because it's more consistent with the tests through the spec.
Let me know if you are OK with leaving the test as is.
Thank you. JoeV
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I don't see the point of writing to the database if we just want to make sure the instance is valid. Also, using FactoryGirl
makes it much more obvious that you're only testing :towhat
and not any other validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the question is: are you testing that it's valid or testing if it raises an exception. If you're testing validity: you don't need to create the record, you can do as @bdunne suggests. If you're testing exception raising, you need to create!
It looks like your testing validity so create!
isn't needed here. It's minor and I know other code does this but it's fine to mix patterns in the specs until you can convert the rest to the new way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I prefer expecting the specific thing (validity) rather than the side effect of rails raising an error when calling create!
on an invalid instance.
spec/models/miq_policy_spec.rb
Outdated
|
||
it 'reports invalid towhat' do | ||
expect do | ||
described_class.create!(:towhat => "BobsYourUncle", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar here
expect(FactoryGirl.build(:miq_policy, :towhat => "BobsYourUncle")).not_to be_valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdunne Here again, if you feel strongly I'd be glad to change this test but for me it's much easier to quickly grasp what the test is doing, that a particular exception is expected and it's more consistent with existing tests in the spec.
Let me know if you are OK with it as is.
Thank you for the review feedback.
JoeV
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be ok with testing create!
if you're concerned with a custom validation message but since this is out of the box validation messages, I think you can just do thing.new
+ thing.valid?
and maybe even thing.errors[:towhat].blank?
or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrafanie Thank you for explaining. Will do.
JoeV
Checked commits jvlcek/manageiq@d344e43~...a9bdbd9 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0 |
Validate towhat policy field (cherry picked from commit 5270416) https://bugzilla.redhat.com/show_bug.cgi?id=1435780
Hammer backport details:
|
https://bugzilla.redhat.com/show_bug.cgi?id=1435780
Invalid values for policy field
towhat
are not recognized, result in the policy beingcreated using the API.
The validations for
mode
were added in [PR 14519](#14519The list of valid values for
towhat
were arrived at by examination of what is supported viathe UI tree_builder_policy.rb
Steps for Testing/QA
Attempt to create a policy with the following data:
e.g.: